<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="迎着朝阳的博客" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    python Flask使用教程 |  迎着朝阳
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="https://dxysun.com/static/yan.png" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  
<script>
var _hmt = _hmt || [];
(function() {
	var hm = document.createElement("script");
	hm.src = "https://hm.baidu.com/hm.js?aa994a8d65700b8835787dd39d079d7e";
	var s = document.getElementsByTagName("script")[0]; 
	s.parentNode.insertBefore(hm, s);
})();
</script>


</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-pythonForFlask"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  python Flask使用教程
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2021/01/31/pythonForFlask/" class="article-date">
  <time datetime="2021-01-30T16:31:03.000Z" itemprop="datePublished">2021-01-31</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">3.3k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">14 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p>python Flask的安装使用教程</p>
<a id="more"></a>

<h1 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h1><p>python版本为python3，安装Flask库。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip3 install flask</span><br></pre></td></tr></table></figure>



<h1 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h1><h2 id="Hello-World"><a href="#Hello-World" class="headerlink" title="Hello World"></a>Hello World</h2><p>创建<code>myapp.py</code>，输入以下内容</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># 导入Flask类</span><br><span class="line">from flask import Flask</span><br><span class="line"># 实例化，可视为固定格式</span><br><span class="line">app &#x3D; Flask(__name__)</span><br><span class="line"></span><br><span class="line"># route()方法用于设定路由</span><br><span class="line">@app.route(&#39;&#x2F;&#39;)</span><br><span class="line">def hello_world():</span><br><span class="line">    return &#39;Hello, World!&#39;</span><br><span class="line"></span><br><span class="line">if __name__ &#x3D;&#x3D; &#39;__main__&#39;:</span><br><span class="line">    # app.run(host, port, debug, options)</span><br><span class="line">    # 默认值：host&#x3D;&quot;127.0.0.1&quot;, port&#x3D;5000, debug&#x3D;False</span><br><span class="line">    app.run(host&#x3D;&quot;0.0.0.0&quot;, port&#x3D;5000)</span><br></pre></td></tr></table></figure>

<p>直接运行该文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python3 myapp.py</span><br></pre></td></tr></table></figure>

<p>然后浏览器访问：<a href="http://127.0.0.1:5000" target="_blank" rel="noopener">http://127.0.0.1:5000</a> 。 得到返回结果<code>Hello, World!</code></p>
<h2 id="get、post用法"><a href="#get、post用法" class="headerlink" title="get、post用法"></a>get、post用法</h2><h3 id="模板文件"><a href="#模板文件" class="headerlink" title="模板文件"></a>模板文件</h3><p>Flask默认到templates目录下查找模板文件，在上边myapp同级目录下创建templates文件夹。</p>
<p>在templates文件夹下创建get.html，写入以下内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">&lt;meta charset&#x3D;&quot;utf-8&quot;&gt;</span><br><span class="line">&lt;title&gt;get请求示例&lt;&#x2F;title&gt;</span><br><span class="line">&lt;&#x2F;head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;form action&#x3D;&quot;&#x2F;deal_request&quot; method&#x3D;&quot;get&quot;&gt;</span><br><span class="line">        &lt;input type&#x3D;&quot;text&quot; name&#x3D;&quot;q&quot; &#x2F;&gt;</span><br><span class="line">        &lt;input type&#x3D;&quot;submit&quot; value&#x3D;&quot;搜索&quot; &#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;form&gt;</span><br><span class="line">&lt;&#x2F;body&gt;</span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>

<p>再在templates文件夹下创建post.html，写入以下内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">&lt;meta charset&#x3D;&quot;utf-8&quot;&gt;</span><br><span class="line">&lt;title&gt;post请求示例&lt;&#x2F;title&gt;</span><br><span class="line">&lt;&#x2F;head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;form action&#x3D;&quot;&#x2F;deal_request&quot; method&#x3D;&quot;post&quot;&gt;</span><br><span class="line">        &lt;input type&#x3D;&quot;text&quot; name&#x3D;&quot;q&quot; &#x2F;&gt;</span><br><span class="line">        &lt;input type&#x3D;&quot;submit&quot; value&#x3D;&quot;搜索&quot; &#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;form&gt;</span><br><span class="line">&lt;&#x2F;body&gt;</span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>

<p>最后在templates文件夹下创建result.html，写入以下内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- Flask 使用Jinja2模板引擎，Jinja2模板引擎源于Django板模所以很多语法和Django是类似的 --&gt;</span><br><span class="line">&lt;h1&gt;&#123;&#123; result &#125;&#125;&lt;&#x2F;h1&gt;</span><br></pre></td></tr></table></figure>



<h3 id="处理方法"><a href="#处理方法" class="headerlink" title="处理方法"></a>处理方法</h3><p>在app.py中添加get_html()、post_html()和deal_request()三个方法，更多说明见注释。当前myapp.py内容如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"># 导入Flask类</span><br><span class="line">from flask import Flask</span><br><span class="line">from flask import render_template</span><br><span class="line">from flask import request</span><br><span class="line"># 实例化，可视为固定格式</span><br><span class="line">app &#x3D; Flask(__name__)</span><br><span class="line"></span><br><span class="line"># route()方法用于设定路由；类似spring路由配置</span><br><span class="line">#等价于在方法后写：app.add_url_rule(&#39;&#x2F;&#39;, &#39;helloworld&#39;, hello_world)</span><br><span class="line">@app.route(&#39;&#x2F;&#39;)</span><br><span class="line">def hello_world():</span><br><span class="line">    return &#39;Hello, World!&#39;</span><br><span class="line"></span><br><span class="line"># 配置路由，当请求get.html时交由get_html()处理</span><br><span class="line">@app.route(&#39;&#x2F;get.html&#39;)</span><br><span class="line">def get_html():</span><br><span class="line">    # 使用render_template()方法重定向到templates文件夹下查找get.html文件</span><br><span class="line">    return render_template(&#39;get.html&#39;)</span><br><span class="line"></span><br><span class="line"># 配置路由，当请求post.html时交由post_html()处理</span><br><span class="line">@app.route(&#39;&#x2F;post.html&#39;)</span><br><span class="line">def post_html():</span><br><span class="line">    # 使用render_template()方法重定向到templates文件夹下查找post.html文件</span><br><span class="line">    return render_template(&#39;post.html&#39;)</span><br><span class="line"></span><br><span class="line"># 配置路由，当请求deal_request时交由deal_request()处理</span><br><span class="line"># 默认处理get请求，我们通过methods参数指明也处理post请求</span><br><span class="line"># 当然还可以直接指定methods &#x3D; [&#39;POST&#39;]只处理post请求, 这样下面就不需要if了</span><br><span class="line">@app.route(&#39;&#x2F;deal_request&#39;, methods &#x3D; [&#39;GET&#39;, &#39;POST&#39;])</span><br><span class="line">def deal_request():</span><br><span class="line">    if request.method &#x3D;&#x3D; &quot;GET&quot;:</span><br><span class="line">        # get通过request.args.get(&quot;param_name&quot;,&quot;&quot;)形式获取参数值</span><br><span class="line">        get_q &#x3D; request.args.get(&quot;q&quot;,&quot;&quot;)</span><br><span class="line">        return render_template(&quot;result.html&quot;, result&#x3D;get_q)</span><br><span class="line">    elif request.method &#x3D;&#x3D; &quot;POST&quot;:</span><br><span class="line">        # post通过request.form[&quot;param_name&quot;]形式获取参数值</span><br><span class="line">        post_q &#x3D; request.form[&quot;q&quot;]</span><br><span class="line">        return render_template(&quot;result.html&quot;, result&#x3D;post_q)</span><br><span class="line"></span><br><span class="line">if __name__ &#x3D;&#x3D; &#39;__main__&#39;:</span><br><span class="line">    # app.run(host, port, debug, options)</span><br><span class="line">    # 默认值：host&#x3D;127.0.0.1, port&#x3D;5000, debug&#x3D;false</span><br><span class="line">    app.run()</span><br></pre></td></tr></table></figure>



<p>运行后访问<code>http://127.0.0.1:5000/get.html</code> <code>http://127.0.0.1:5000/post.html</code>查看运行效果。</p>
<h3 id="返回json数据的用法"><a href="#返回json数据的用法" class="headerlink" title="返回json数据的用法"></a>返回json数据的用法</h3><p>以json等格式（而非以前的表单格式）发起请求，并以json等格式（而非以前的html）进行响应。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"># 导入Flask类</span><br><span class="line">from flask import Flask, jsonify</span><br><span class="line">from flask import render_template</span><br><span class="line">from flask import request</span><br><span class="line"></span><br><span class="line"># 实例化，可视为固定格式</span><br><span class="line">app &#x3D; Flask(__name__)</span><br><span class="line"># 调试模式，设置FLASK_DEBUG环境变量，并将值设置为1。或者设置app.debug&#x3D;True,修改代码，会发现这次Flask会自动重启</span><br><span class="line"># app.debug &#x3D; True</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># route()方法用于设定路由；类似spring路由配置</span><br><span class="line"># 等价于在方法后写：app.add_url_rule(&#39;&#x2F;&#39;, &#39;helloworld&#39;, hello_world)</span><br><span class="line">@app.route(&#39;&#x2F;&#39;)</span><br><span class="line">def hello_world():</span><br><span class="line">    return &#39;Hello, World!&#39;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">@app.route(&#39;&#x2F;hello&#39;)</span><br><span class="line">def hello():</span><br><span class="line">    return &#39;Hello, Flask!&#39;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 配置路由，当请求get.html时交由get_html()处理</span><br><span class="line">@app.route(&#39;&#x2F;get.html&#39;)</span><br><span class="line">def get_html():</span><br><span class="line">    # 使用render_template()方法重定向到templates文件夹下查找get.html文件</span><br><span class="line">    return render_template(&#39;get.html&#39;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 配置路由，当请求post.html时交由post_html()处理</span><br><span class="line">@app.route(&#39;&#x2F;post.html&#39;)</span><br><span class="line">def post_html():</span><br><span class="line">    # 使用render_template()方法重定向到templates文件夹下查找post.html文件</span><br><span class="line">    return render_template(&#39;post.html&#39;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 配置路由，当请求deal_request时交由deal_request()处理</span><br><span class="line"># 默认处理get请求，我们通过methods参数指明也处理post请求</span><br><span class="line"># 当然还可以直接指定methods &#x3D; [&#39;POST&#39;]只处理post请求, 这样下面就不需要if了</span><br><span class="line">@app.route(&#39;&#x2F;deal_request&#39;, methods&#x3D;[&#39;GET&#39;, &#39;POST&#39;])</span><br><span class="line">def deal_request():</span><br><span class="line">    if request.method &#x3D;&#x3D; &quot;GET&quot;:</span><br><span class="line">        # get通过request.args.get(&quot;param_name&quot;,&quot;&quot;)形式获取参数值</span><br><span class="line">        get_q &#x3D; request.args.get(&quot;q&quot;, &quot;&quot;)</span><br><span class="line">        return render_template(&quot;result.html&quot;, result&#x3D;get_q)</span><br><span class="line">    elif request.method &#x3D;&#x3D; &quot;POST&quot;:</span><br><span class="line">        # post通过request.form[&quot;param_name&quot;]形式获取参数值</span><br><span class="line">        post_q &#x3D; request.form[&quot;q&quot;]</span><br><span class="line">        return render_template(&quot;result.html&quot;, result&#x3D;post_q)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">@app.route(&#39;&#x2F;rest_test&#39;, methods&#x3D;[&#39;POST&#39;])</span><br><span class="line">def hello_world1():</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    通过request.json以字典格式获取post的内容</span><br><span class="line">    通过jsonify实现返回json格式</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    post_param &#x3D; request.json</span><br><span class="line">    result_dict &#x3D; &#123;</span><br><span class="line">        &quot;result_code&quot;: 2000,</span><br><span class="line">        &quot;post_param&quot;: post_param</span><br><span class="line">    &#125;</span><br><span class="line">    return jsonify(result_dict)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if __name__ &#x3D;&#x3D; &#39;__main__&#39;:</span><br><span class="line">    # app.run(host, port, debug, options)</span><br><span class="line">    # 默认值：host&#x3D;127.0.0.1, port&#x3D;5000, debug&#x3D;false</span><br><span class="line">    app.run(host&#x3D;&quot;127.0.0.1&quot;, port&#x3D;5000, debug&#x3D;True)</span><br></pre></td></tr></table></figure>



<p>使用curl进行请求</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -H &quot;Content-Type:application&#x2F;json&quot; -X POST --data &#39;&#123;&quot;username&quot;: &quot;ls&quot;,&quot;password&quot;:&quot;toor&quot;&#125;&#39; http:&#x2F;&#x2F;127.0.0.1:5000&#x2F;rest_test</span><br></pre></td></tr></table></figure>

<p>得到返回结果</p>
<p><img src="https://tu.dxysun.com/20210131005906-20210131005906.png" alt=""></p>
<p>更多相关使用参考<a href="https://dormousehole.readthedocs.io/en/latest/" target="_blank" rel="noopener">官方文档</a></p>
<h1 id="服务端部署"><a href="#服务端部署" class="headerlink" title="服务端部署"></a>服务端部署</h1><h2 id="使用uwsgi在服务端启动应用"><a href="#使用uwsgi在服务端启动应用" class="headerlink" title="使用uwsgi在服务端启动应用"></a>使用uwsgi在服务端启动应用</h2><p>uwsgi 若为安装，使用如下命令进行安装</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip3 install uwsgi</span><br></pre></td></tr></table></figure>

<h3 id="命令行启动"><a href="#命令行启动" class="headerlink" title="命令行启动"></a>命令行启动</h3><p>uwsgi 是基于 python 模块中的 WSGI 调用的。示例中 Flask 应用名称为 app.py ， 使用如下命令在5000端口启动</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">uwsgi --http-socket 127.0.0.1:5000 --manage-script-name --mount &#x2F;app&#x3D;myapp:app</span><br></pre></td></tr></table></figure>

<p><code>--manage-script-name</code> 会把 <code>SCRIPT_NAME</code> 处理移向 uwsgi ， 因为 uwsgi 会更智能一些。与 <code>--mount</code> 联用可以把向 <code>/app</code> 发送的请求 重定向到 <code>myapp:app</code> 。如果应用可以在根级别访问，那么可以使用单个 <code>/</code> 来代替 <code>/app</code> 。 <code>myapp</code> 指 flask 应用的文件名称（不含扩展 名）或者提供 <code>app</code> 的模块名称。 <code>app</code> 在应用内部可被调用 （通常是 <code>app = Flask(__name__)</code>）</p>
<h3 id="配置文件启动"><a href="#配置文件启动" class="headerlink" title="配置文件启动"></a>配置文件启动</h3><p>命令行启动在控制台窗口关闭时应用也就停止运行了，配置文件启动可以使应用运行在后台，不随着控制台窗口的关闭而停止运行。</p>
<p>在项目目录下创建<code>uwsgi.ini</code>文件，内容如下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[uwsgi]</span><br><span class="line">http-socket &#x3D; 127.0.0.1:5000</span><br><span class="line">module &#x3D; myapp:app</span><br><span class="line">daemonize &#x3D; .&#x2F;uwsgi.log</span><br><span class="line">pidfile&#x3D;uwsgi.pid</span><br><span class="line">#以下根据需要配置</span><br><span class="line">#manage-script-name</span><br><span class="line">#mount &#x3D; &#x2F;app&#x3D;app:app</span><br></pre></td></tr></table></figure>

<p>使用如下命令启动</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">uwsgi --ini uwsgi.ini</span><br></pre></td></tr></table></figure>

<p>启动后在项目目录下生成<code>uwsgi.log</code>和<code>uwsgi.pid</code>，分别用于存储运行日志和运行的进程id</p>
<p>整体目录结构如下</p>
<p><img src="https://tu.dxysun.com/20210131012256-20210131012257.png" alt=""></p>
<p>停止命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">uwsgi --stop uwsgi.pid</span><br></pre></td></tr></table></figure>



<h2 id="使用gunicorn在服务端启动应用"><a href="#使用gunicorn在服务端启动应用" class="headerlink" title="使用gunicorn在服务端启动应用"></a>使用gunicorn在服务端启动应用</h2><p>Gunicorn 是一个 UNIX 下的 WSGI HTTP 服务器，它是一个 移植自 Ruby 的 Unicorn 项目的 pre-fork worker 模型。</p>
<p>安装gunicorn</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install gunicorn</span><br></pre></td></tr></table></figure>

<h3 id="命令行启动-1"><a href="#命令行启动-1" class="headerlink" title="命令行启动"></a>命令行启动</h3><p>在 Gunicorn 上运行 Flask 应用非常简单:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gunicorn myapp:app</span><br></pre></td></tr></table></figure>

<p>使用更多参数运行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gunicorn -w 4 -b 127.0.0.1:4000 myapp:app</span><br></pre></td></tr></table></figure>

<blockquote>
<p>使用 4 worker 进程（ <code>-w 4</code> ）来运行 Flask 应用，绑定到 localhost 的 4000 端口（ <code>-b 127.0.0.1:4000</code> ）</p>
</blockquote>
<p><code>gunicorn</code> 命令需要应用或者包的名称和应用实例。如果使用工厂模式，那么可以传递一个调用来实现:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gunicorn &quot;myproject:create_app()&quot;</span><br></pre></td></tr></table></figure>



<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">-c CONFIG    : CONFIG,配置文件的路径，通过配置文件启动；生产环境使用；</span><br><span class="line">-b ADDRESS   : ADDRESS，ip加端口，绑定运行的主机；</span><br><span class="line">-w INT, --workers INT：用于处理工作进程的数量，为正整数，默认为1；</span><br><span class="line">-k STRTING, --worker-class STRTING：要使用的工作模式，默认为sync异步，可以下载eventlet和gevent并指定</span><br><span class="line">--threads INT：处理请求的工作线程数，使用指定数量的线程运行每个worker。为正整数，默认为1。</span><br><span class="line">--worker-connections INT：最大客户端并发数量，默认情况下这个值为1000。</span><br><span class="line">--backlog int：未决连接的最大数量，即等待服务的客户的数量。默认2048个，一般不修改；</span><br><span class="line">-p FILE, --pid FILE：设置pid文件的文件名，如果不设置将不会创建pid文件</span><br><span class="line">--access-logfile FILE   ：  要写入的访问日志目录</span><br><span class="line">--access-logformat STRING：要写入的访问日志格式</span><br><span class="line">--error-logfile FILE, --log-file FILE  ：  要写入错误日志的文件目录。</span><br><span class="line">--log-level LEVEL   ：   错误日志输出等级。</span><br><span class="line">--limit-request-line INT   ：  HTTP请求头的行数的最大大小，此参数用于限制HTTP请求行的允许大小，默认情况下，这个值为4094。值是0~8190的数字。</span><br><span class="line">--limit-request-fields INT   ：  限制HTTP请求中请求头字段的数量。此字段用于限制请求头字段的数量以防止DDOS攻击，默认情况下，这个值为100，这个值不能超过32768</span><br><span class="line">--limit-request-field-size INT  ：  限制HTTP请求中请求头的大小，默认情况下这个值为8190字节。值是一个整数或者0，当该值为0时，表示将对请求头大小不做限制</span><br><span class="line">-t INT, --timeout INT：超过这么多秒后工作将被杀掉，并重新启动。一般设定为30秒；</span><br><span class="line">--daemon： 是否以守护进程启动，默认false；</span><br><span class="line">--chdir： 在加载应用程序之前切换目录；</span><br><span class="line">--graceful-timeout INT：默认情况下，这个值为30，在超时(从接收到重启信号开始)之后仍然活着的工作将被强行杀死；一般使用默认；</span><br><span class="line">--keep-alive INT：在keep-alive连接上等待请求的秒数，默认情况下值为2。一般设定在1~5秒之间。</span><br><span class="line">--reload：默认为False。此设置用于开发，每当应用程序发生更改时，都会导致工作重新启动。</span><br><span class="line">--spew：打印服务器执行过的每一条语句，默认False。此选择为原子性的，即要么全部打印，要么全部不打印；</span><br><span class="line">--check-config   ：显示现在的配置，默认值为False，即显示。</span><br><span class="line">-e ENV, --env ENV： 设置环境变量；</span><br></pre></td></tr></table></figure>



<h3 id="配置文件启动-1"><a href="#配置文件启动-1" class="headerlink" title="配置文件启动"></a>配置文件启动</h3><p>gunicorn.conf</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"># 并行工作进程数</span><br><span class="line">workers &#x3D; 4</span><br><span class="line"># 指定每个工作者的线程数</span><br><span class="line">threads &#x3D; 2</span><br><span class="line"># 监听内网端口5000</span><br><span class="line">bind &#x3D; &#39;127.0.0.1:5000&#39;</span><br><span class="line"># 设置守护进程,后台运行</span><br><span class="line">daemon &#x3D; True</span><br><span class="line"># 工作模式协程</span><br><span class="line">worker_class &#x3D; &#39;gevent&#39;</span><br><span class="line"># 设置最大并发量</span><br><span class="line">worker_connections &#x3D; 2000</span><br><span class="line"># 设置进程文件目录</span><br><span class="line">pidfile &#x3D; &#39;&#x2F;var&#x2F;run&#x2F;gunicorn.pid&#39;</span><br><span class="line"># 设置访问日志和错误信息日志路径</span><br><span class="line">accesslog &#x3D; &#39;&#x2F;var&#x2F;log&#x2F;gunicorn_acess.log&#39;</span><br><span class="line">errorlog &#x3D; &#39;&#x2F;var&#x2F;log&#x2F;gunicorn_error.log&#39;</span><br><span class="line"># 设置日志记录水平</span><br><span class="line">loglevel &#x3D; &#39;warning&#39;</span><br></pre></td></tr></table></figure>

<p>启动</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gunicorn -c gunicorn.conf myapp:app</span><br></pre></td></tr></table></figure>



<h2 id="配置-nginx"><a href="#配置-nginx" class="headerlink" title="配置 nginx"></a>配置 nginx</h2><p>官网给的nginx配置如下</p>
<p>一个 nginx 的基本 uWSGI 配置如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">location &#x3D; &#x2F;yourapplication &#123; rewrite ^ &#x2F;yourapplication&#x2F;; &#125;</span><br><span class="line">location &#x2F;yourapplication &#123; try_files $uri @yourapplication; &#125;</span><br><span class="line">location @yourapplication &#123;</span><br><span class="line">  include uwsgi_params;</span><br><span class="line">  uwsgi_pass unix:&#x2F;tmp&#x2F;yourapplication.sock;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这个配置把应用绑定到 <code>/yourapplication</code> 。如果你想要在根 URL 下运行应用 非常简单:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">location &#x2F; &#123; try_files $uri @yourapplication; &#125;</span><br><span class="line">location @yourapplication &#123;</span><br><span class="line">    include uwsgi_params;</span><br><span class="line">    uwsgi_pass unix:&#x2F;tmp&#x2F;yourapplication.sock;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>官网给的是使用sock文件进行访问，这里我们使用端口访问，简化配置如下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location &#x2F;app &#123;</span><br><span class="line">            proxy_pass   http:&#x2F;&#x2F;127.0.0.1:5000&#x2F;;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>这里配置把应用绑定到 <code>/app</code></p>
</blockquote>
<p> 修改nginx的配置文件后，使用如下命令重启nginx即可</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nginx -s reload</span><br></pre></td></tr></table></figure>





<h1 id="Flask与Django比较"><a href="#Flask与Django比较" class="headerlink" title="Flask与Django比较"></a>Flask与Django比较</h1><p>FLask和Django都是Python比较流行的web框架，进行一下比较</p>
<table>
<thead>
<tr>
<th>比较项</th>
<th>Django</th>
<th>Flask</th>
<th>复杂度比较</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>项目创建</td>
<td>Django需要用命令创建项目</td>
<td>Flask直接编写文件就可运行</td>
<td>Django复杂</td>
<td>Django需要用命令创建项目是因为要创建出整个项目框架</td>
</tr>
<tr>
<td>路由</td>
<td>Django使用专门的urls.py文件</td>
<td>Flask直接使用@app.route()</td>
<td>Django笨重</td>
<td>Django类似Strut2的配置Flask类似Spring的配置，Flask感觉更好</td>
</tr>
<tr>
<td>get和post</td>
<td>request.GET[‘name’]和request.POST[“name”]</td>
<td>request.args.get(“name”,””)和request.form[“q”]</td>
<td>差不多</td>
<td>Flask格式上不统一</td>
</tr>
<tr>
<td>restful</td>
<td>使用django-resful框架</td>
<td>使用jsonify</td>
<td>差不多</td>
<td>Flask不需要单建一个app，更直观一些</td>
</tr>
<tr>
<td>数据库操作</td>
<td>django集成了对数据库的操作</td>
<td>Flask没集成对数据库的操作要另行直连或使用sqlalchemy</td>
<td>差不多</td>
<td>django复杂很大程度来源于对数据库的集成。</td>
</tr>
</tbody></table>
<p>Django是Python最流行的Web框架但配置比较复杂，Flask是一个轻量级的框架配置比较简单如果项目比较小推荐使用Flask。</p>
<p>进一步来说，Flask的轻量来源其“暂时不用的功能都先不做处理”，Django复杂来源于其“可能用到的功能都先集成”</p>
<h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1><p><a href="https://www.cnblogs.com/lsdb/p/10488448.html" target="_blank" rel="noopener">Python3+Flask安装使用教程</a></p>
<p><a href="https://dormousehole.readthedocs.io/en/latest/" target="_blank" rel="noopener">Flask中文文档</a></p>
<p><a href="https://flask.palletsprojects.com/en/1.1.x/" target="_blank" rel="noopener">Flask官网</a></p>
<p><a href="https://mastersword.cn/paper/89e42a4b.html" target="_blank" rel="noopener">在Centos7使用uWSGI部署Flask</a></p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://dxysun.com/2021/01/31/pythonForFlask/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python/" rel="tag">python</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2021/01/31/nginxForInlets/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            内网穿透工具ngrok和inlets，轻松将本地服务暴露到公网
          
        </div>
      </a>
    
    
      <a href="/2021/01/30/pythonForDyld/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Mac下python dyld ：Library not loaded 问题解决</div>
      </a>
    
  </nav>

  
   
  
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2024
        <i class="ri-heart-fill heart_icon"></i> dxysun
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
        <li>
          <a href="https://beian.miit.gov.cn" target="_black" rel="nofollow">豫ICP备17012675号-1</a>
        </li>
        
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="https://dxysun.com/static/logo.png" alt="迎着朝阳"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/photos">相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://tu.dxysun.com/alipay-20201219151322.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://tu.dxysun.com/weixin-20201219151346.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


<script src="/js/dz.js"></script>



    
  </div>
</body>

</html>